package com.app.juyanresturantapp;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;


import com.app.juyanresturantapp.bean.DetailBean;
import com.app.juyanresturantapp.bean.FoodsBean;
import com.google.gson.Gson;
import com.squareup.picasso.Picasso;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class ProductDetailActivity extends AppCompatActivity {

    TextView detailName,detailLabel,detaiPrice,detailDecription;
    ImageView detaiPic;
    String id;
    String TAG = "ProductDetailActivity";
    DetailBean.ListDTO list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_detail);
        initView();
        initData();
        getData();
    }

    private void getData() {
        //创建Client
        OkHttpClient client = new OkHttpClient();
        RequestBody body = new FormBody.Builder()
                .add("id", id)
                .build();
        //创建Request对象
        final Request request = new Request.Builder()
                .url(getResources().getString(R.string.url)+"/app14/food/findone")
                .post(body)
                .build();

        final Call call = client.newCall(request);

        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e(TAG,"网络访问失败");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String data = response.body().string();
                Gson gson = new Gson();
                DetailBean detailBean = gson.fromJson(data, DetailBean.class);
                if (detailBean.isSuccess()) {
                   list = detailBean.getList();
                   Log.e(TAG,"Fsddf");
                    setData();
                }else {
                    Log.e(TAG,"数据不存在");
                }
            }
        });

    }

    private void setData() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Picasso.with(getApplicationContext()).load(getResources().getString(R.string.url)+list.getIcon()).into(detaiPic);
            }
        });

        detailName.setText(String.valueOf(list.getName()));
        detailLabel.setText(String.valueOf(list.getLabel()));
        detailDecription.setText(String.valueOf(list.getDescription()));
        detaiPrice.setText(String.valueOf(list.getPrice())+"元/份");


    }

    private void initData() {
      Intent intent = getIntent();
        id = intent.getStringExtra("id");
    }

    private void initView() {
        detailName = findViewById(R.id.detailName);
        detailLabel = findViewById(R.id.detailLabel);
        detailDecription = findViewById(R.id.detailDecription);
        detaiPrice = findViewById(R.id.detaiPrice);
        detaiPic = findViewById(R.id.detaiPic);
    }
}